package com.eqdd.yiqidian.ui.yiqiyue;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.box.ItemDecoration.SectionDividerItemDecoration;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ArticleMenuListBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.eqdd.yiqidian.utils.DialogUtil;
import com.eqdd.yiqidian.utils.HttpUtil;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.SwipeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-12-13  下午1:05.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class YueMenuListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private SlimAdapterEx<ArticleMenuListBean> slimAdapterEx;

    @Override
    public void initBinding() {

        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "文集");
        initTopRightText("新建", v -> {

            Bundle bundle = new Bundle();
            bundle.putString(Config.TITLE, "新建文集");
            bundle.putString("tip", "文集名字");
            bundle.putString(Config.CONTENT, "");
            startActivityForResult(EditTextActivity.class, bundle, Config.EDIT_TEXT);
        });
    }

    @Override
    public void initData() {
        RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> {
            refresh();
        });
        SwipeUtil.openSwipe(this, dataBinding.recyclerView, (menuBridge, pos) -> {
            HttpUtil.deleteArticleMenu(YueMenuListActivity.this, slimAdapterEx.getDataItem(pos).getId(), () -> {
                menuBridge.closeMenu();
                slimAdapterEx.getData().remove(pos);
                slimAdapterEx.notifyItemRemoved(pos);
            });
        });
        dataBinding.recyclerView.setSwipeItemLongClickListener((view, position) -> {
            DialogUtil.edit(YueMenuListActivity.this, "修改名称", slimAdapterEx.getDataItem(position).getArticleName(), "文集名", (dialog, which) -> {
                String newName = dialog.getInputEditText().getText().toString().trim();
                HttpUtil.updateArticleMenuName(YueMenuListActivity.this,
                        slimAdapterEx.getDataItem(position).getId(), newName
                        ,
                        () -> {
                            slimAdapterEx.getDataItem(position).setArticleName(newName);
                            slimAdapterEx.notifyItemChanged(position);
                        });
            });
        });
        dataBinding.recyclerView.setSwipeItemClickListener((view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Config.ID, slimAdapterEx.getDataItem(position).getId());
            bundle.putString(Config.TITLE, slimAdapterEx.getDataItem(position).getArticleName());
            startActivity(YueArticleListActivity.class, bundle);
        });
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_01, new SlimInjector<ArticleMenuListBean>() {
            @Override
            public void onInject(ArticleMenuListBean data, IViewInjector injector) {

                injector.text(R.id.tv_name, data.getArticleName());

            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        dataBinding.coolRefreshView.setRefreshing(true);
    }


    private void refresh() {
        OkGo.<HttpResult<List<ArticleMenuListBean>>>post(HttpConfig.BASE_URL + HttpConfig.YUE_ARTICLE_MENU)
                .params("userGuid", AppConstent.user.getGuid())
                .execute(new DialogCallBack<HttpResult<List<ArticleMenuListBean>>>(YueMenuListActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<ArticleMenuListBean>>> response) {
                        dataBinding.coolRefreshView.setRefreshing(false);
                        HttpResult<List<ArticleMenuListBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }

                    @Override
                    public void onError(Response<HttpResult<List<ArticleMenuListBean>>> response) {
                        super.onError(response);
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }
                });
    }


    @Override
    public void setView() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.EDIT_TEXT && resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra(Config.CONTENT);
            HttpUtil.addArticleMenu(this, content, () -> dataBinding.coolRefreshView.setRefreshing(true));
        }

    }


}
